package com.chaoxing.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class EncryptUtil {
    private static final String KEY = "u2oh6Vu^HWe40fj";
    private static final String ALGORITHM = "DES/ECB/PKCS5Padding";
    
    public static String encryptPassword(String password) {
        try {
            // DES密钥必须是8字节
            byte[] keyBytes = KEY.getBytes(StandardCharsets.UTF_8);
            byte[] keyData = new byte[8];
            System.arraycopy(keyBytes, 0, keyData, 0, Math.min(keyBytes.length, 8));
            
            SecretKeySpec keySpec = new SecretKeySpec(keyData, "DES");
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            
            byte[] encrypted = cipher.doFinal(password.getBytes(StandardCharsets.UTF_8));
            
            // 转换为16进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : encrypted) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            throw new RuntimeException("密码加密失败", e);
        }
    }
}